import pagination from './pagination'
import dataDic from '@/mixins/dataDic'
import { cloneDeep } from 'lodash'

export default {
  mixins: [pagination, dataDic],
  data() {
    return {
      loading: false,
      queryListParams: {},
      d2crudOptions: {
        size: 'mini',
        stripe: true,
        // border: true,
        height: '100%',
      },
    }
  },
  created() {
    this.queryList()
  },
  methods: {
    // 获取数据
    async queryList() {
      if (!this.pageApis) {
        this.$message.error('请配置pageApis.list()查询接口')
      }
      this.loading = true
      this.initQueryParams()
      try {
        let res = await this.pageApis.list(this.queryListParams)
        // console.log(res)
        this.loading = false
        this.dataList = []
        this.pagination.dataCount = res.total
        this.dataList = res.result
      } catch (e) {
        this.loading = false
        console.error(e)
      }
    },
    // 重置查询
    async queryReset() {
      // 重置queryForm
      for (let key in this.queryForm) {
        let flag = this.resetQueryFormFields.includes(key)
        if (flag) {
          this.$set(this.queryForm, key, '')
        }
      }
      // 重置分页
      this.pagination.pageNum = 1
      this.pagination.currentPage = 1
      this.queryList()
    },
    // 初始化查询参数
    initQueryParams() {
      this.queryListParams = {
        queryData: { ...this.queryForm },
        size: this.pagination.pageSize,
        current: this.pagination.pageNum,
      }
      if (this.queryForm.keyword) {
        this.queryListParams.keyword = this.queryForm.keyword
      }
      delete this.queryListParams.queryData.keyword
      if (this.queryForm.pickerData && this.queryForm.pickerData.length > 0) {
        this.queryListParams.startData = this.queryForm.pickerData[0]
        this.queryListParams.endData = this.queryForm.pickerData[1]
      }
    },
    // 打开弹出框
    openDialog(ref, row) {
      let d;
      if (row) {
        d = cloneDeep(row)
      } else {
        d = ''
      }
      this.$refs[ref].open(d)
    },
    // 编辑表格行
    rowUpdate({ index, row }) {
      this.openDialog('createUpdateDialog', row)
    },
    // 删除行
    rowDelete({ index, row }) {
      if (!this.pageApis) {
        this.$message.error('请配置pageApis.remove()查询接口')
      }
      this.$confirm('此操作不可逆, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.pageApis.remove(row)
        this.$message.success('删除成功！')
        this.queryList();
      }).catch(() => { });
    }
  },
}
